package org.jsola.hr.web;


import org.apache.commons.io.FilenameUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.hr.common.ExcelKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.query.SocStatisticalQuery;
import org.jsola.hr.service.ISocStatisticalService;
import org.jsola.hr.vo.EmpSocInfoListVO;
import org.jsola.hr.vo.EmpSocInfoStatisticalExportVO;
import org.jsola.hr.vo.SocAmountVO;
import org.jsola.hr.vo.excel.ContractExportVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author lyh
 * 社保统计页面
 */

@RestController("hrSocStatisticalController")
@RequestMapping(HrConstants.API_PATH + "/v1/socStatistical")
public class SocStatisticalController implements ISocStatisticalControllerAPI {

    @Autowired
    private ISocStatisticalService socStatisticalService;

    @Override
    @GetMapping(value = "/selectAmount")
    public Result<SocAmountVO> selectAmount(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socStatisticalService.selectAmount(socStatisticalQuery,tokenUser.getSiteId())
        );

    }

    @Override
    @GetMapping(value = "/selectMonthStatistical")
    public Result<List<SocAmountVO>> selectMonthStatistical(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socStatisticalService.selectMonthStatistical(socStatisticalQuery,tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectCompanyStatistical")
    public Result<List<SocAmountVO>> selectCompanyStatistical(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socStatisticalService.selectCompanyStatistical(socStatisticalQuery,tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectChildCompanyStatistical")
    public Result<List<SocAmountVO>> selectChildCompanyStatistical(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socStatisticalService.selectChildCompanyStatistical(socStatisticalQuery,tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectEmpSocInfoStatistical")
    public Result<Page<EmpSocInfoListVO>> selectEmpSocInfoStatistical(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socStatisticalService.selectEmpSocInfoStatistical(socStatisticalQuery,tokenUser.getSiteId())
        );

    }

    @Override
    @GetMapping(value = "/selectCountAndProportion")
    public Result<SocAmountVO> selectCountAndProportion(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socStatisticalService.selectCountAndProportion(socStatisticalQuery,tokenUser.getSiteId())
        );

    }

    @Override
    @GetMapping(value = "/selectSocProgItem")
    public Result<SocAmountVO> selectSocProgItem(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socStatisticalService.selectSocProgItem(socStatisticalQuery,tokenUser.getSiteId())
        );

    }

    @Override
    @GetMapping(value = "/exportEmpSocInfo")
    public ResponseEntity<Resource> empSocInfoStatisticalExport(SocStatisticalQuery socStatisticalQuery) {
        TokenUser tokenUser = UserContext.getUser();
        //查询到导出的数据
        List<EmpSocInfoStatisticalExportVO> empSocInfoStatisticalExportVOS = socStatisticalService.empSocInfoStatisticalExport(socStatisticalQuery, tokenUser.getSiteId());
        // 导出excel
        String excelFilePath = ExcelKit.exportExcel("empSocInfo", "社保信息", EmpSocInfoStatisticalExportVO.class, empSocInfoStatisticalExportVOS);
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8),
                StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }
}
